import { userEntrySchema, type IReqCreateUser, type IRspUser } from "@packages/shared";
import useModelValid, { type FormValidType } from "./valid.hook";
import { ref } from "vue";
import const_API from "./http.hook";


const useUser = () => {

    type UserModel = {
        username: FormValidType;
        password: FormValidType;
        age: FormValidType;
        gender: FormValidType;
        phone: FormValidType;
        nickName: FormValidType;
    }

    const userModel = ref<UserModel>({
        username: {
            value: 'admin',
            message: ''
        },
        password: {
            value: '123456',
            message: ''
        },
        age: {
            value: 18,
            message: ''
        },
        gender: {
            value: '男',
            message: ''
        },
        phone: {
            value: '13987182312',
            message: ''
        },
        nickName: {
            value: 'D来D去',
            message: ''
        }
    })

    const { doValidField, doValidAll } = useModelValid(userModel, userEntrySchema)

    const handleReset = () => {
        userModel.value = {
            username: {
                value: '',
                message: ''
            },
            password: {
                value: '',
                message: ''
            },
            age: {
                value: 0,
                message: ''
            },
            gender: {
                value: '',
                message: ''
            },
            phone: {
                value: '',
                message: ''
            },
            nickName: {
                value: '',
                message: ''
            }
        }
    }

    const handleClearField = (field: keyof UserModel) => {
        const target = userModel.value[field];
        if (target && 'value' in target) {
            target.value = typeof target.value === 'number' ? 0 : '';
        }
    };

    const tipModel = ref({
        alertType: '',
        msg: '',
    })

    const onSuccess = <IRspUser>(data: IRspUser) => {
        console.log('onSuccess=>', data);
        tipModel.value.msg = '注册成功'
        tipModel.value.alertType = 'success'
    }
    const onBusinessError = (msg: string) => {
        console.log('onBusinessError=>', msg);
        tipModel.value.msg = msg
        tipModel.value.alertType = 'warning'
    }
    const onError = (error: any) => {
        console.log('onError=>', error);
        tipModel.value.msg = '注册失败'
        tipModel.value.alertType = 'error'
    }

    const handleSubmit = () => {
        const { ok = false, result = undefined } = doValidAll()
        if (!ok || result === undefined) {
            tipModel.value.msg = '请检查输入内容'
            tipModel.value.alertType = 'warning'
            return
        }

        const_API.registerNewUser({
            param: result as IReqCreateUser,
            onSuccess,
            onBusinessError,
            onError
        })
    };

    return {

        userModel,
        tipModel,

        doValid: doValidField,
        handleReset,
        handleClearField,
        handleSubmit,
    }



}

export default useUser